import { chatActonType } from '../actionTypes';
import { chatContextType, IChatDialogueMessageSchema } from '@/types/chat';
import { askGPT } from '@/api/chat';
import { AppDispatch } from '@/store';
import data2stram from '@/utils/dataTostream';
export const getAllHistoryChatAction = (id: number) => ({
  type: chatActonType.GET_ALL_HISTORY,
  payload: id,
});

export const clearAllHistoryChatAction = () => ({
  type: chatActonType.CLEAR_ALL_HISTORY,
});

export const delHistoryChatAction = (id: string) => ({
  type: chatActonType.DEL_HISTORY,
  payload: id,
});

export const addDialogDetail = (chatContext: chatContextType) => ({
  type: chatActonType.ADD_DETAIL,
  payload: chatContext,
});

export const addHistory = (IChatDialogueMessage: IChatDialogueMessageSchema) => ({
  type: chatActonType.ADD_HISTORY,
  payload: IChatDialogueMessage,
});

export const updateHistory = (id: string, input: string) => {
  return async (dispatch: AppDispatch) => {
    const res: any = await askGPT(input);
    if (res.ok) {
      data2stram(res, dispatch, chatActonType.UPDATE_HISTORY, id);
    }
  };
};

export const updateCurrentDialog = (id: string, input: string) => {
  return async (dispatch: AppDispatch) => {
    const res: any = await askGPT(input);
    if (res.ok) {
      data2stram(res, dispatch, chatActonType.UPDATE_CURRENTDIALOG, id);
    }
  };
};
